package com.eunion.web.cms.entity.spec;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.springframework.data.jpa.domain.Specification;

import com.eunion.web.cms.entity.RecruitStudentsPlan;


public class RecruitStudentsPlanSpec {
    public static final int HIGH_RATE = 1;
    public static final int MIDDLE_RATE = 2;
    public static final int LOW_RATE = 3;
    public static Specification<RecruitStudentsPlan> build(RecruitStudentsPlan queryBean) {
        return new RecruitStudentsPlanQuery(queryBean);
    }

    public static Specification<RecruitStudentsPlan> build(RecruitStudentsPlan queryBean,boolean accurate) {
        return new RecruitStudentsPlanQuery(queryBean,accurate);
    }

    public static Specification<RecruitStudentsPlan> build(RecruitStudentsPlan queryBean,int rate) {
        return new RecruitStudentsPlanQuery(queryBean,rate);
    }

    private static class RecruitStudentsPlanQuery implements Specification<RecruitStudentsPlan> {

        private RecruitStudentsPlan queryBean;
        private boolean accurate;
        private int rate;

        private RecruitStudentsPlanQuery(RecruitStudentsPlan queryBean) {
            this.queryBean = queryBean;
        }

        private RecruitStudentsPlanQuery(RecruitStudentsPlan queryBean,boolean accurate) {
            this.queryBean = queryBean;
            this.accurate = accurate;
        }

        private RecruitStudentsPlanQuery(RecruitStudentsPlan queryBean,int rate) {
            this.queryBean = queryBean;
            this.rate = rate;
        }

        @Override
        public Predicate toPredicate(Root<RecruitStudentsPlan> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            List<Predicate> list = getPredicates(root, query, cb, queryBean,accurate,rate);
            Predicate[] preList = new Predicate[list.size()];
            return cb.and(list.toArray(preList));
        }

    }

    private static List<Predicate> getPredicates(Root<RecruitStudentsPlan> root, CriteriaQuery<?> query, CriteriaBuilder cb,
            RecruitStudentsPlan queryBean,boolean accurate,int rate) {
        // 条件集合
        List<Predicate> list = new ArrayList<>();

        //年份
        String year = queryBean.getYear();
        //省份
        String provinceName = queryBean.getProvinceName();
        //大学名称
        String collegeName = queryBean.getCollegeName();
        //大学编码
        String collegeCode = queryBean.getCollegeCode();
        
        int batch = queryBean.getBatch()== null?0:queryBean.getBatch();
        int direction = queryBean.getDirection() == null?1:queryBean.getDirection();
        if(accurate) {
            if (StringUtils.isNotBlank(year)) {
                list.add(cb.equal(root.get("year"),year.trim()));
            }
            if (StringUtils.isNotBlank(provinceName)) {
                list.add(cb.equal(root.get("provinceName"),provinceName.trim()));
            }
            if (StringUtils.isNotBlank(collegeCode)) {
                list.add(cb.equal(root.get("collegeCode"),collegeCode.trim()));
            }
            if (batch != 0) {
                list.add(cb.equal(root.get("batch"),batch));
            }
            list.add(cb.equal(root.get("direction"), direction));
        } else {
            if (StringUtils.isNotBlank(year)) {
                list.add(cb.equal(root.get("year"),year.trim()));
            }
            if (StringUtils.isNotBlank(provinceName)) {
                list.add(cb.equal(root.get("provinceName"),provinceName.trim()));
            }
            if (StringUtils.isNotBlank(collegeCode)) {
                list.add(cb.equal(root.get("collegeCode"),collegeCode.trim()));
            }
            if(StringUtils.isNotBlank(collegeName)) {
                list.add(cb.like(root.get("collegeName"),collegeName.trim()));
            }
            if (batch != 0) {
                list.add(cb.equal(root.get("batch"),batch));
            }
            list.add(cb.equal(root.get("direction"), direction));
            if(queryBean.getLowestScore() > 0) {
                switch (rate) {
                    case HIGH_RATE:
                        // list.add(cb.greaterThan(root.get("lowestScore"), queryBean.getLowestScore()-10));
                        list.add(cb.lessThanOrEqualTo(root.get("lowestScore"), queryBean.getLowestScore()-5));
                        break;
                    case MIDDLE_RATE:
                        list.add(cb.greaterThan(root.get("lowestScore"), queryBean.getLowestScore()-5));
                        list.add(cb.lessThanOrEqualTo(root.get("lowestScore"), queryBean.getLowestScore()+5));
                        break;
                    case LOW_RATE:
                        list.add(cb.greaterThan(root.get("lowestScore"), queryBean.getLowestScore()+5));
                        list.add(cb.lessThanOrEqualTo(root.get("lowestScore"), queryBean.getLowestScore()+10));
                        break;
                        default:
                            break;
                }
            }
        }


        return list;
    }
}
